﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Yipusoft.Evaluate.DBUtility;
using Yipusoft.Evaluate.Model;

namespace Yipusoft.Evaluate.DAL
{
    public class Award
    {
        public bool Exsits(int AwardID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@AwardID",SqlDbType.Int)
            };
            parameters[0].Value = AwardID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Awards_Exsits", parameters);
                if (dr.Read())
                    return true;
                else
                    return false;
            }
        }
        public int Insert(AwardInfo Award)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@AwardID",SqlDbType.Int),
                new SqlParameter("@AwardName",SqlDbType.NVarChar,50),
                new SqlParameter("@AwardDate",SqlDbType.DateTime),
                new SqlParameter("@TotalScore",SqlDbType.Decimal),
                new SqlParameter("@Description",SqlDbType.NVarChar,500)
            };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = Award.AwardName;
            parameters[2].Value = Award.AwardDate;
            parameters[3].Value = Award.TotalScore;
            parameters[4].Value = Award.Description;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Awards_Insert", parameters);
                return (int)parameters[0].Value;
            }
        }
        public bool Update(AwardInfo Award)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@AwardID",SqlDbType.Int),
                new SqlParameter("@AwardName",SqlDbType.NVarChar,50),
                new SqlParameter("@AwardDate",SqlDbType.DateTime),
                new SqlParameter("@TotalScore",SqlDbType.Decimal),
                new SqlParameter("@Description",SqlDbType.NVarChar,500)
            };
            parameters[0].Value = Award.AwardID;
            parameters[1].Value = Award.AwardName;
            parameters[2].Value = Award.AwardDate;
            parameters[3].Value = Award.TotalScore;
            parameters[4].Value = Award.Description;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Awards_Update", parameters);
                return rowAffected > 0;
            }
        }
        public bool Delete(int AwardID)
        {
            SqlParameter[] parameters = {
                new SqlParameter("@AwardID",SqlDbType.Int)
            };
            parameters[0].Value = AwardID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                int rowAffected = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Awards_Delete", parameters);
                return rowAffected > 0;
            }
        }
        public AwardInfo GetAward(int AwardID)
        {
            AwardInfo Award = null;
            SqlParameter[] parameters = {
                new SqlParameter("@AwardID",SqlDbType.Int)
            };
            parameters[0].Value = AwardID;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Awards_GetDetail", parameters);
                if (dr.Read())
                {
                    Award = new AwardInfo();
                    Award.AwardID = dr.GetInt32(0);
                    Award.AwardName = dr.GetString(1);
                    Award.AwardDate = dr.GetDateTime(2);
                    Award.TotalScore = dr.GetDecimal(3);
                    Award.Description = dr.GetString(4);
                }
                return Award;
            }
        }
        public List<AwardInfo> GetList(string StrWhere)
        {
            List<AwardInfo> Awards = new List<AwardInfo>();
            AwardInfo Award = null;
            SqlParameter[] parameters = {
                new SqlParameter("@StrWhere",SqlDbType.NVarChar,4000)
            };
            parameters[0].Value = StrWhere;
            using (SqlConnection conn = new SqlConnection(SqlHelper.CONN_STRING))
            {
                SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "Awards_GetList", parameters);
                while (dr.Read())
                {
                    Award = new AwardInfo();
                    Award.AwardID = dr.GetInt32(0);
                    Award.AwardName = dr.GetString(1);
                    Award.AwardDate = dr.GetDateTime(2);
                    Award.TotalScore = dr.GetDecimal(3);
                    Award.Description = dr.GetString(4);
                    Awards.Add(Award);
                }
                return Awards;
            }
        }
    }
}
